home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / synthia / playmidi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  15.2 KB  |  700 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * Copyright (C) 1991, Silicon Graphics, Inc.
  19.  * All Rights Reserved.
  20.  */
  21.  
  22.     /*
  23.      *  playmidi  -  Play a MIDI file on a Hollywood/Magnum audio
  24.      *
  25.      *  Contains routines:
  26.      *
  27.      *    playtracks (aout, ntrks, tracks)
  28.      *    wf_init ()
  29.      *    read_ssf (file, samplebuf)
  30.      *    wl_init ()
  31.      *
  32.      *  Jim Bennett
  33.      *  1991
  34.      */
  35.  
  36. #include <audio.h>
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "midif.h"
  41. #include "paths.h"
  42.  
  43. extern    float    current_volume;
  44. extern    int    current_voice;
  45.  
  46. extern    int    dographics;
  47. extern    int    maxvoices;
  48. extern    int    instruments [INSTRUMENTS];
  49. extern    int    keydowns [];        /* Global state of key presses    */
  50.  
  51.     /*
  52.      *  Table of currently active voices
  53.      */
  54.  
  55. static struct s_voice    voices [2*MAX_VOICES];
  56.  
  57. static    short    sbuf [SAMPLE_BUFSIZE];
  58.  
  59.     /*
  60.      *  Waveform tables, for quick lookup of waveforms
  61.      */
  62.  
  63. static    float    waveforms [5] [WFTABL*WF_REPLICATION];
  64. static    float    sinwave [WFTABL*WF_REPLICATION];
  65.  
  66.     /*
  67.      *  Per instrument pointers for waveform and amplitude
  68.      */
  69.  
  70. float    *inst_wf [INSTRUMENTS];
  71. float    *inst_ae [INSTRUMENTS];
  72. float    *inst_off_ae [INSTRUMENTS];
  73.  
  74.     /*
  75.      *  Amplitude envelopes (time, value pairs, terminated with
  76.      *  a value of 0.0)
  77.      */
  78.  
  79. static    float    bell_key_down [] = {
  80.     0.01, 1.0,
  81.     0.2, 0.6,
  82.     0.75, 0.0
  83.     };
  84.  
  85. static    float    bell_key_up [] = {
  86.     0.03, 0.0
  87.     };
  88.  
  89. static    float    flute_key_down [] = {
  90.     0.04, 0.5,
  91.     0.4, 1.0,
  92.     1.0, 0.0
  93.     };
  94.  
  95. static    float    flute_key_up [] = {
  96.     0.01, 0.0
  97.     };
  98.  
  99. static    float    horn_key_down [] = {
  100.     0.02, 0.8,
  101.     0.04, 0.3,
  102.     0.06, 0.9,
  103.     0.18, 0.7,
  104.     0.30, 1.0,
  105.     0.8, 0.0
  106.     };
  107.  
  108. static    float    horn_key_up [] = {
  109.     0.01, 0.0
  110.     };
  111.  
  112.     /*
  113.      *  Wavelength table, for conversion of MIDI pitch -> wavelength
  114.      */
  115.  
  116. static    int    wltab [256];
  117. static    float    wlistab [256];
  118.  
  119.     /*
  120.      *  Attenuate both volume and waveform with frequency
  121.      */
  122.  
  123. static    float    volume_attenuate [256];
  124. static    float    waveform_attenuate [256];
  125.  
  126.  
  127.     /*
  128.      *  playtracks  -  Generate tones for all of the tracks
  129.      */
  130.  
  131. playtracks (aout, ntrks, tracks)
  132.  
  133.     ALport    aout;
  134.     int    ntrks;
  135.     struct    s_ptrk    *tracks;
  136.  
  137.     {
  138.     struct    s_mfevent *midi_event_ptr;
  139.     int    instrument, note, active;
  140.     int    double_maxvoices;
  141.     int    key_event;
  142.     int    total_voices;
  143.     double    t;
  144.     float    tupdate;
  145.     float    tdel;
  146.     float    evtime;
  147.     float    amp;
  148.     int    i, j;
  149.     int    lsamp, rsamp, wl, wli;
  150.     long    event;
  151.     struct    s_voice    *freevoice;
  152.     struct    s_voice *vp;
  153.     struct    s_ptrk    *trkp;
  154.     struct    s_finger *fp;
  155.     struct    s_finger *oldfp;
  156.     int    sbuf_ix = 0;
  157.  
  158. /* Initialize instruments for each track.    */
  159.  
  160.     printf ("\n");
  161.     for (i=0; i<INSTRUMENTS; i++)
  162.         {
  163.         inst_wf[i] = waveforms[i%5];
  164.         if ((i%3) == 0)
  165.             {
  166.             inst_ae[i] = flute_key_down;
  167.             inst_off_ae[i] = flute_key_up;
  168.             }
  169.         else if ((i%3) == 1)
  170.             {
  171.             inst_ae[i] = bell_key_down;
  172.             inst_off_ae[i] = bell_key_up;
  173.             }
  174.         else
  175.             {
  176.             inst_ae[i] = horn_key_down;
  177.             inst_off_ae[i] = horn_key_up;
  178.             }
  179.         }
  180.  
  181.     total_voices = 0;
  182.     for (i=0; i<ntrks; i++)
  183.         {
  184.         midi_event_ptr = tracks[i].base;
  185.         tracks[i].evptr = midi_event_ptr;
  186.  
  187.         instrument = (midi_event_ptr->event >> 24) & 0x0f;
  188.         if (midi_event_ptr->time < LONG_TIME)
  189.             {
  190.             printf ("Track %2d: %2d fingers, instrument %2d\n",
  191.                 i, tracks[i].nfingers, instrument);
  192.             if (total_voices >= maxvoices)
  193.                 midi_event_ptr->time = END_TIME;
  194.             else
  195.                 total_voices += tracks[i].nfingers;
  196.             }
  197.  
  198.     /* Reassign instruments as directed    */
  199.  
  200.         instrument = instruments[instrument];
  201.  
  202.         tracks[i].on_amp_env = inst_ae [instrument];
  203.         tracks[i].off_amp_env = inst_off_ae [instrument];
  204.  
  205.         for (j=0; j<2*MAX_FINGERS; j++)
  206.             {
  207.             tracks[i].f[j].vp = NULL;
  208.             tracks[i].f[j].finger = j;
  209.             tracks[i].f[j].track = i;
  210.             tracks[i].f[j].wftab = inst_wf [instrument];
  211.             }
  212.         }
  213.  
  214. /* Initialize active voices list (place all voices on free list)    */
  215.  
  216.     active = 0;
  217.     freevoice = voices;
  218.     for (i=0; i<2*MAX_VOICES; i++)
  219.         {
  220.         voices[i].fp = NULL;
  221.         voices[i].next = &voices[i+1];
  222.         }
  223.     voices[(2*MAX_VOICES)-1].next = NULL;
  224.  
  225. /* Tracks initialized, start at the begining of time and loop until end    */
  226.  
  227.     t = 0.0;
  228.     tdel = 1.0 / (double)SYNTHIA_RATE;
  229.     tupdate = 0.0;
  230.     double_maxvoices = 2*maxvoices;
  231.     key_event = 0;
  232.  
  233.     while (1)
  234.         {
  235.  
  236.     /* Get the time of the next event, exit if end of time    */
  237.  
  238.         evtime = END_TIME;
  239.         for (i=0,trkp=tracks; i<ntrks; i++,trkp++)
  240.             if ((trkp->evptr)->time < evtime)
  241.                 evtime = (trkp->evptr)->time;
  242.  
  243.         if (evtime > LONG_TIME)
  244.             {
  245.             if (sbuf_ix > 0)
  246.                 ALwritesamps (aout, sbuf, sbuf_ix);
  247.             return;
  248.             }
  249.  
  250.     /* To quantize events somewhat:            */
  251.     /*    evtime += 0.003;            */
  252.  
  253.     /* Loop until time passes next event time    */
  254.  
  255.         for (; t<evtime; t+= tdel, tupdate += tdel)
  256.             {
  257.             if (dographics && (tupdate > 0.05))
  258.                 {
  259.                 tupdate = 0.0;
  260.                 check_input ();
  261.                 if (key_event)
  262.                     {
  263.                     key_event = 0;
  264.                     hilight_keys (&keydowns[(MIDDLE_C-24)]);
  265.                     }
  266.                 }
  267.             lsamp = 0;
  268.             rsamp = 0;
  269.             for (i=0,vp=voices; i<double_maxvoices; i++,vp++)
  270.                 {
  271.                 if (vp->fp)
  272.                 {
  273.                 fp = vp->fp;
  274.  
  275.             /* Process voice    */
  276.  
  277.                 if (fp->track & 0x01)
  278.                     {
  279.                     lsamp = lsamp +
  280.     (fp->atten * (fp->val * fp->wftab [(int)(fp->wli*fp->wlis)])) +
  281.     (fp->catten * (fp->val * sinwave [(int)(fp->wli*fp->wlis)]));
  282.                     }
  283.                 else
  284.                     {
  285.                     rsamp = rsamp +
  286.     (fp->atten * (fp->val * fp->wftab [(int)(fp->wli*fp->wlis)])) +
  287.     (fp->catten * (fp->val * sinwave [(int)(fp->wli*fp->wlis)]));
  288.                     }
  289.                 fp->wli++;
  290.                 if (fp->wli >= fp->wl) fp->wli = 0;
  291.                 fp->t += tdel;
  292.                 fp->val += fp->vdel;
  293.                 if (fp->t >= *fp->aeptr)
  294.                     {
  295.                     fp->aeptr++;
  296.                     if (*fp->aeptr > 0.01)
  297.                         {
  298.     fp->val = *fp->aeptr++;
  299.     fp->vdel = tdel *
  300.     (fp->aeptr[1] - fp->aeptr[-1]) / (fp->aeptr[0] - fp->aeptr[-2]);
  301.                         }
  302.                         else
  303.                         {
  304.     fp->vp = NULL;        /* Turn this voice off    */
  305.     vp->fp = NULL;
  306.     vp->next = freevoice;
  307.     freevoice = vp;
  308.     if (vp->keydown)
  309.     {
  310.     key_event = 1;
  311.     keydowns[fp->note]--;
  312.     active--;
  313.     }    
  314.                         }
  315.                     }
  316.                 }
  317.                 }
  318.             sbuf[sbuf_ix++] = lsamp;
  319.             sbuf[sbuf_ix++] = rsamp;
  320.             if (sbuf_ix >= SAMPLE_BUFSIZE)
  321.                 {
  322.                 ALwritesamps (aout, sbuf, sbuf_ix);
  323.                 sbuf_ix = 0;
  324.                 }
  325.             }
  326.  
  327.     /* Initiate events    */
  328.  
  329.         for (i=0,trkp=tracks; i<ntrks; i++,trkp++)
  330.             {
  331.             while ((trkp->evptr)->time <= evtime)
  332.                 {
  333.                 event = (trkp->evptr)->event;
  334.                 note = event&0x7f;
  335.                 fp = trkp->f;
  336.                 fp = &fp[(event >> 28) & 0x0f];
  337.  
  338.                 if (((event >> 16)&0xff) == NOTE_ON)
  339.                     {
  340.                     if (active < maxvoices)
  341.                         {
  342. #ifdef DEBUG
  343.     if (fp->vp)
  344.     printf ("# %d\n", (event >> 28) & 0x0f);
  345. #endif
  346.                         freevoice->fp = fp;
  347.                         freevoice->keydown = 1;
  348.                         fp->vp = freevoice;
  349.                         freevoice = freevoice->next;
  350.                         fp->note = note;
  351.                         fp->wl = wltab[note];
  352.  
  353.                         key_event = 1;
  354.                         keydowns[note]++;
  355.                         active++;
  356.  
  357.                         fp->wlis = wlistab[note];
  358.                         fp->atten =
  359.                             waveform_attenuate[note];
  360.                         fp->catten = 1.0 - fp->atten;
  361.                         amp =
  362. volume_attenuate[note] * current_volume * (5000 + ((event>>8)&0x7f)*100);
  363.                         fp->atten = amp*fp->atten;
  364.                         fp->catten = amp*fp->catten;
  365.  
  366.                         fp->aeptr = trkp->on_amp_env;
  367.                         if (current_voice != -1)
  368.                             {
  369.                             fp->wftab =
  370.                             inst_wf[current_voice];
  371.                             fp->aeptr =
  372.                             inst_ae[current_voice];
  373.                             }
  374.  
  375.                         fp->wli = 0;
  376.                         fp->t = 0.0;
  377.                         fp->val = 0.0;
  378.                         fp->vdel = tdel *
  379.                          (fp->aeptr[1] / fp->aeptr[0]);
  380.                         }
  381.                     }
  382.                 else
  383.                     {    /* Else NOTE_OFF event    */
  384.                     if (fp->vp)
  385.                         {
  386.                         vp = fp->vp;
  387.                         oldfp = fp;
  388.                         oldfp->vp = NULL;
  389.                         fp = &fp[MAX_FINGERS];
  390.                         if (fp->vp)
  391.     {
  392. #ifdef DEBUG
  393.     printf ("- %d %d\n", fp->finger, fp->vp->fp->finger);
  394. #endif
  395.  
  396. /* Previous note still lingering, shut it down    */
  397.  
  398.     fp->vp->fp = NULL;
  399.     fp->vp->next = freevoice;
  400.     freevoice = fp->vp;
  401.     fp->vp = NULL;
  402.     }
  403.                         vp->fp = fp;
  404.                         vp->keydown = 0;
  405.                         fp->vp = vp;
  406.                         fp->wl = oldfp->wl;
  407.  
  408.                         key_event = 1;
  409.                         keydowns[note]--;
  410.                         active--;
  411.  
  412.                         fp->wlis = oldfp->wlis;
  413.                         fp->atten = oldfp->atten;
  414.                         fp->catten = oldfp->catten;
  415.  
  416.                         fp->aeptr = trkp->off_amp_env;
  417.                         if (current_voice != -1)
  418.                           {
  419.                           fp->wftab =
  420.                             inst_wf[current_voice];
  421.                           fp->aeptr =
  422.                             inst_off_ae[current_voice];
  423.                           }
  424.  
  425.                         fp->wli = oldfp->wli;
  426.                         fp->t = 0.0;
  427.                         fp->val = oldfp->val;
  428.                         fp->vdel = tdel *
  429.                 ((fp->aeptr[1] - fp->val) / fp->aeptr[0]);
  430.                         }
  431.                     }
  432.                 trkp->evptr++;
  433.                 }
  434.             }
  435.         }
  436.     }
  437.  
  438.     /*
  439.      *  wf_init  -  Initialize waveform tables
  440.      *
  441.      *    wf_init generates 5 synthetic waveforms, and
  442.      *    then overwrites these with sampled waveforms,
  443.      *    if they are found.
  444.      */
  445.  
  446. wf_init ()
  447.  
  448.     {
  449.     int    i, j;
  450.     int    tmod = WF_REPLICATION*WFTABL;
  451.  
  452. /* Table 0: Sin wave with 1/6, 1/3, and 1/2 harmonics    */
  453.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  454.         sinwave[i] =
  455.             0.01 * sin((i*2.0*M_PI)/(WFTABL*6)) +
  456.             0.02 * sin((i*2.0*M_PI)/(WFTABL*3)) +
  457.             0.03 * sin((i*2.0*M_PI)/(WFTABL*2)) +
  458.             0.85 * sin((i*2.0*M_PI)/WFTABL);
  459.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  460.         sinwave[i] +=
  461. 0.01 * (((drand48() + drand48() + drand48() + drand48()) / 2.0) - 1.0);
  462.  
  463. /* Apply smoother    */
  464.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  465.         waveforms[0][i] = 0.05*sinwave[(i+tmod-2)%tmod] +
  466.                   0.20*sinwave[(i+tmod-1)%tmod] +
  467.                   0.30*sinwave[i] +
  468.                   0.20*sinwave[(i+1)%tmod];
  469.                   0.05*sinwave[(i+2)%tmod];
  470.  
  471.  
  472. /* Table 1: Spiky wave            */
  473.     for (j=0; j<WF_REPLICATION; j++)
  474.         {
  475.         for (i=0; i<WFTABL/5; i++)
  476.             sinwave[(j*WFTABL)+i] = 0.0;
  477.         for (; i<(2*WFTABL)/5; i++)
  478.             sinwave[(j*WFTABL)+i] =
  479.                 ((float)(i-WFTABL/5) * 5.0) / (float)WFTABL;
  480.         for (; i<(3*WFTABL)/5; i++)
  481.             sinwave[(j*WFTABL)+i] =
  482.                 1.0 - (((float)(i-(2*WFTABL)/5) * 10.0) /
  483.                         (float)WFTABL);
  484.         for (; i<(4*WFTABL)/5; i++)
  485.             sinwave[(j*WFTABL)+i] =
  486.                 (((float)(i-(3*WFTABL)/5) * 5.0) /
  487.                         (float)WFTABL) - 1.0;
  488.         for (; i<WFTABL; i++)
  489.             sinwave[(j*WFTABL)+i] = 0.0;
  490.         }
  491.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  492.         sinwave[i] +=
  493. 0.02 * (((drand48() + drand48() + drand48() + drand48()) / 2.0) - 1.0);
  494.  
  495. /* Apply smoother    */
  496.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  497.         waveforms[1][i] = 0.05*sinwave[(i+tmod-2)%tmod] +
  498.                   0.20*sinwave[(i+tmod-1)%tmod] +
  499.                   0.30*sinwave[i] +
  500.                   0.20*sinwave[(i+1)%tmod];
  501.                   0.05*sinwave[(i+2)%tmod];
  502.  
  503.  
  504. /* Table 2: Saw tooth wave with 1/2 harmonic    */
  505.     for (j=0; j<WF_REPLICATION; j++)
  506.         {
  507.         for (i=0; i<WFTABL/5; i++)
  508.             sinwave[(j*WFTABL)+i] =
  509.                 (((float)i * 10.0) / (float)WFTABL) - 1.0;
  510.         for (; i<WFTABL; i++)
  511.             sinwave[(j*WFTABL)+i] =
  512.                 1.0 - (((i-WFTABL/5) * 10.0) /
  513.                         (4.0 * (float)WFTABL));
  514.         }
  515.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  516.         sinwave[i] +=
  517.             0.02 * sin((i*2.0*M_PI)/(WFTABL*2));
  518.  
  519. /* Apply smoother    */
  520.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  521.         waveforms[2][i] = 0.05*sinwave[(i+tmod-2)%tmod] +
  522.                   0.20*sinwave[(i+tmod-1)%tmod] +
  523.                   0.30*sinwave[i] +
  524.                   0.20*sinwave[(i+1)%tmod];
  525.                   0.05*sinwave[(i+2)%tmod];
  526.  
  527. /* Table 3: Square wave with 1/6, 1/3, and 1/2 sin harmonics    */
  528.     for (j=0; j<WF_REPLICATION; j++)
  529.         {
  530.         for (i=0; i<WFTABL/5; i++)
  531.             sinwave[(j*WFTABL)+i] = -1.0;
  532.         for (; i<(3*WFTABL)/10; i++)
  533.             sinwave[(j*WFTABL)+i] =
  534.                 ((float)(i-WFTABL/5)*20.0 /
  535.                     (float)WFTABL) - 1.0;
  536.         for (; i<(7*WFTABL)/10; i++)
  537.             sinwave[(j*WFTABL)+i] = 1.0;
  538.         for (; i<(8*WFTABL)/10; i++)
  539.             sinwave[(j*WFTABL)+i] =
  540.                 1.0 - ((float)(i-(7*WFTABL)/10)*20.0 /
  541.                         (float)WFTABL);
  542.         for (; i<WFTABL; i++)
  543.             sinwave[(j*WFTABL)+i] = -1.0;
  544.         }
  545.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  546.         sinwave[i] =
  547.             0.80 * sinwave[i] +
  548.             0.00 * sin((i*2.0*M_PI)/(WFTABL*6)) +
  549.             0.01 * sin((i*2.0*M_PI)/(WFTABL*3)) +
  550.             0.02 * sin((i*2.0*M_PI)/(WFTABL*2)) +
  551. 0.02 * (((drand48() + drand48() + drand48() + drand48()) / 2.0) - 1.0);
  552.  
  553. /* Apply smoother    */
  554.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  555.         waveforms[3][i] = 0.05*sinwave[(i+tmod-2)%tmod] +
  556.                   0.20*sinwave[(i+tmod-1)%tmod] +
  557.                   0.30*sinwave[i] +
  558.                   0.20*sinwave[(i+1)%tmod];
  559.                   0.05*sinwave[(i+2)%tmod];
  560.  
  561.  
  562. /* Table 4: Sin wave with 1/6, 1/3, and 1/2 harmonics    */
  563.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  564.         sinwave[i] =
  565.             0.01 * sin((i*2.0*M_PI)/(WFTABL*6)) +
  566.             0.02 * sin((i*2.0*M_PI)/(WFTABL*3)) +
  567.             0.03 * sin((i*2.0*M_PI)/(WFTABL*2)) +
  568.             0.85 * sin((i*2.0*M_PI)/WFTABL);
  569.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  570.         sinwave[i] +=
  571. 0.01 * (((drand48() + drand48() + drand48() + drand48()) / 2.0) - 1.0);
  572.  
  573. /* Apply smoother    */
  574.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  575.         waveforms[4][i] = 0.05*sinwave[(i+tmod-2)%tmod] +
  576.                   0.20*sinwave[(i+tmod-1)%tmod] +
  577.                   0.30*sinwave[i] +
  578.                   0.20*sinwave[(i+1)%tmod];
  579.                   0.05*sinwave[(i+2)%tmod];
  580.  
  581.  
  582. /* We have generated the waves, now check for samples    */
  583.  
  584.     read_ssf ("0.ssf", waveforms[0]);
  585.     read_ssf ("1.ssf", waveforms[1]);
  586.     read_ssf ("2.ssf", waveforms[2]);
  587.     read_ssf ("3.ssf", waveforms[3]);
  588.     read_ssf ("4.ssf", waveforms[4]);
  589.  
  590. /* Then put a sin wave into sinwave (at 1/2 frequency)    */
  591.  
  592.     for (i=0; i<WF_REPLICATION*WFTABL; i++)
  593.         sinwave[i] = sin((i*2.0*M_PI)/(WFTABL*2));
  594.     }
  595.  
  596.     /*
  597.      *  read_ssf  -  Read in waveform from file into buffer
  598.      *
  599.      *    Returns 1 if error
  600.      */
  601.  
  602. read_ssf (fname, fbuf)
  603.  
  604.     char    *fname;
  605.     float    *fbuf;
  606.  
  607.     {
  608.     FILE    *ssf;
  609.     char    *buf;
  610.     char    *path;
  611.     int    ret;
  612.     float    samprate;
  613.     float    incount;
  614.     int    icnt;
  615.  
  616.     ssf = fopen (fname, "r");
  617.     if ((ssf == NULL) && (*fname != '/'))
  618.         {
  619.         if (!(path = getenv("SYNTHIA_SOUND_PATH")))
  620.             path = SOUND_PATH;
  621.         buf = (char *)malloc (strlen(fname) + strlen(path) + 2);
  622.         strcpy (buf, path);
  623.         strcat (buf, "/");
  624.         strcat (buf, fname);
  625.         ssf = fopen (buf, "r");
  626.         }
  627.     if (ssf == NULL) return (1);
  628.  
  629.     ret = fread (&samprate, sizeof(float), 1, ssf);
  630.     if (ret != 1) return (1);
  631.  
  632.     ret = fread (&incount, sizeof(float), 1, ssf);
  633.     if (ret != 1) return (1);
  634.  
  635.     icnt = incount;
  636.     if (icnt != WF_REPLICATION*WFTABL) return (1);
  637.  
  638.     ret = fread (fbuf, sizeof(float), icnt, ssf);
  639.     if (ret != icnt) return (1);
  640.  
  641. /* File read into sample buffer, all is OK    */
  642.  
  643.     return (0);
  644.     }
  645.  
  646.     /*
  647.      *  wl_init  -  Initialize wavelength table
  648.      */
  649.  
  650. wl_init ()
  651.  
  652.     {
  653.     int    i, j;
  654.     int    hz;
  655.     double    exptab[12];        /* For the 12th roots of two    */
  656.     float    dhz;
  657.  
  658.     for (i=0; i<12; i++)
  659.         exptab[i] = pow (2.0, (double)i/12.0);
  660.     for (i=0; i<(A_ABOVE-36); i++)        /* Tones too low    */
  661.         {
  662.         wltab[i] = 0;
  663.         wlistab[i] = 0.0;
  664.         volume_attenuate[i] = 1.0;
  665.         waveform_attenuate[i] = 1.0;
  666.         }
  667.  
  668. /* Start at 3 octaves below A (the A above middle C)        */
  669. /* Each octave is 12 halftones, and the frequency doubles    */
  670.  
  671.     for (hz=(A_HZ>>3), i=(A_ABOVE-36); i<128; i+=12, hz *= 2)
  672.         {
  673.         for (j=0; j<12; j++)
  674.             {
  675.             dhz = hz * exptab[j];
  676.             wltab[i+j] = (float)SYNTHIA_RATE/dhz;
  677.             wlistab[i+j] = (float)WFTABL/(float)wltab[i+j];
  678.             wltab[i+j] *= WF_REPLICATION;
  679.  
  680.             if ((i+j) >= MIDDLE_C+48)
  681.                 volume_attenuate[i+j] = 0.0;
  682.             else
  683.             if ((i+j) > MIDDLE_C)
  684.                 volume_attenuate[i+j] = 1.0 -
  685.                     ((float)((i+j)-MIDDLE_C) / 48.0);
  686.             else
  687.                 volume_attenuate[i+j] = 1.0;
  688.  
  689.             if ((i+j) >= MIDDLE_C+30)
  690.                 waveform_attenuate[i+j] = 0.0;
  691.             else
  692.             if ((i+j) > MIDDLE_C-6)
  693.                 waveform_attenuate[i+j] = 1.0 -
  694.                     ((float)((i+j)-(MIDDLE_C-6)) / 36.0);
  695.             else
  696.                 waveform_attenuate[i+j] = 1.0;
  697.             }
  698.         }
  699.     }
  700.